package com.zkk.qqserver.service;

import com.zkk.qqcommon.Message;
import com.zkk.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 该类对应的对象和某个客户端保持通信
 * */
public class ServerConnectClientThread extends Thread {

    private Socket socket;
    private String userId;//连接到服务器的用户ID

    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    @Override
    public void run() {  //这里线程处于run的状态，可以发送/接收 消息
        while (true){
            try {
                System.out.println("服务端和客户端" + userId + "保持通信，读取数据...");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();
                //根据message对象的消息类型，做出相应的业务处理
                if (message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)){
                    //收到客户端请求 在线用户列表 的信息
                    System.out.println(message.getSender() + "要在线用户列表");
                    String onlineUser = ManageClientThreads.getOnlineUser();
                    //构建一个message对象  返回给客户端
                    Message message1 = new Message();
                    message1.setMesType(MessageType.MESSAGE_RET__ONLINE_FRIEND);
                    message1.setContent(onlineUser);
                    message1.setGetter(message.getSender());
                    //写入数据通道
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(message1);

                }else if(message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)){
                    System.out.println(message.getSender() + "退出");
                    //将客户端对应的线程从集合删除
                    ManageClientThreads.removeSeverConnectClientThread(message.getSender());
                    socket.close();//关闭连接
                    //退出线程
                    break;
                }else if(message.getMesType().equals(MessageType.MESSAGE_COMM_MES)){
                    //根据message获取getter id 再获取对应的线程
                    ServerConnectClientThread serverConnectClientThread = ManageClientThreads.getServerConnectClientThread(message.getGetter());
                    //得到对应Socket的输出流对象，将Message对象转发给指定客户端
                    ObjectOutputStream oos = new ObjectOutputStream(serverConnectClientThread.socket.getOutputStream());
                    oos.writeObject(message);

                }else if(message.getMesType().equals(MessageType.MESSAGE_TO_ALL)){

                    for (int i = 0; i < ManageClientThreads.getHm().size(); i++) {
                        String[] s = ManageClientThreads.getOnlineUser().split(" ");
                       //群发不发给自己
                        if (!(s[i].equals(message.getSender()))){
                            ServerConnectClientThread connectClientThread = ManageClientThreads.getServerConnectClientThread(s[i]);
                            ObjectOutputStream oos = new ObjectOutputStream(connectClientThread.socket.getOutputStream());
                            oos.writeObject(message);
                        }

                    }
                }else if(message.getMesType().equals(MessageType.MESSAGE_FILE)){

                    //根据getterId获得对应线程  将message对象转发
                    ServerConnectClientThread connectClientThread = ManageClientThreads.getServerConnectClientThread(message.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(connectClientThread.socket.getOutputStream());
                    oos.writeObject(message);

                }else {
                    System.out.println("other");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
